package com.lynn.salary.web.action;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.lynn.salary.dao.ICommonDao;
import com.lynn.salary.dto.SearchDTO;
import com.lynn.salary.util.Cache;
import com.lynn.salary.util.DataProvider;
import com.lynn.salary.util.ObjectFactory;
import com.lynn.salary.util.Page;
import com.lynn.salary.util.PageParase;

public class CommonAction extends BaseAction {
	private Page page;
	private List<Map<String, Object>> data;
	private String pageName;
	
	private String searchTag;

	@Override
	public String execute() throws Exception {

		pageName = handlePageName(pageName);
		InputStream is = CommonAction.class.getResourceAsStream("/" + pageName + ".xml");
		page = PageParase.instance.paraseFromStream(is);
		DataProvider dp = page.getDataProvider();
		
		Object searchCondition = Cache.instance.getSearchConditionByTag(searchTag);
		
		try {
			if (dp.getMethod().toUpperCase().equals("SQL") && searchCondition==null) {
				System.out.println("data Privider >>>>>>> SQL");
				ICommonDao dao = (ICommonDao) ObjectFactory.getBean("dao");
				List<Map<String, Object>> t = dao.executeSQL(handlerSql(dp.getTotalSql()));
				totalCount = ((Long) t.get(0).get("totalCount")).intValue();

				data = dao.executeSQL(handlerSql(dp.getListSql()));
//		} else if (dp.getMethod().toUpperCase().equals("SERVICE")) {
			} else if (searchCondition!=null) {
				System.out.println("data Privider >>>>>>> "+dp.getServiceName()+"." +dp.getServiceMethod());
				Object service = ObjectFactory.getBean(dp.getServiceName());
				Class c = service.getClass();
				Method m = c.getMethod(dp.getServiceMethod(),new Class[]{Object.class});
				data = (List<Map<String, Object>>) m.invoke(service,handlerCondition(searchCondition));
				Map<String, Object> r = data.remove(0);
				totalCount = ((Long) r.get("totalCount")).intValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
			totalCount = -1;
			data = new ArrayList<Map<String, Object>>();
		}
		this.currentPage = pageNum;
		return SUCCESS;
	}
	
	private Object handlerCondition(Object searchCondition) {
		SearchDTO dto = (SearchDTO) searchCondition;
		dto.setStartIdx("" + (pageNum - 1) * numPerPage);
		dto.setNumPerPage("" + numPerPage);
		dto.setLoginUserId("'"+loginUser.getLoginUser().getId()+"'");
		return dto;
	}

	private String handlePageName(String pn) {
		if(pn.startsWith("allArticleType")) {
			return "allArticleType";
		}
		if(pn.startsWith("seeAllArticle")) {
			return "seeAllArticle";
		}
		if(pn.startsWith("allSubjectComment")) {
			return "allSubjectComment";
		}
		if(pn.startsWith("allSubject")) {
			return "allSubject";
		}
		if(pn.startsWith("jhSubject")) {
			return "jhSubject";
		}
		if(pn.startsWith("topSubject")) {
			return "topSubject";
		}
		if(pn.startsWith("userMgt")) {
			return "userMgt";
		}
		if(pn.startsWith("sysArgument")) {
			return "sysArgument";
		}
		if(pn.startsWith("allAccessory")) {
			return "allAccessory";
		}
		
		return pn;
	}

	private String handlerSql(String listSql) {
		listSql = listSql.replace("{startIdx}", "" + (pageNum - 1)
				* numPerPage);
		listSql = listSql.replace("{numPerPage}", "" + numPerPage);
		listSql = listSql.replace("{loginUserId}", "'"+loginUser.getLoginUser().getId()+"'");
		return listSql;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Map<String, Object>> getData() {
		return data;
	}

	public void setData(List<Map<String, Object>> data) {
		this.data = data;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public String getSearchTag() {
		return searchTag;
	}

	public void setSearchTag(String searchTag) {
		this.searchTag = searchTag;
	}

}
